Object API submit

Object内置对象 API

1 Object.getPrototypeOf( obj ) ,返回obj对象的构造函数的prototype属性(也就是obj对象的 proto 属性对象),每个构造函数都有一个prototype属性

1
2
3
4
5
6
7
8
function Test (){};
var test = new Test();
console.log(Object.getPrototypeOf(test));
console.log(Object.getPrototypeOf(test) === Test.prototype);//true
//-------------------------------------------------------------------------
var obj = new Object ();
console.log(Object.getPrototypeOf(obj));
console.log(Object.getPrototypeOf(obj) == Object.prototype);//true

2 isPrototypeOf( )

3 设置对象的原型属性 proto

3.1 Object.create( proto , prop ) 用来设置对象的 proto 的属性的指向;返回一个创建的对象;prop和Object.defineProperties(obj ,prop) 里面的prop格式一致,如下栗子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var obj = Object.create({ foo: 1 }, { // foo is on obj's prototype chain.
bar: {
value: 2 // bar is a non-enumerable property.
},
baz: {
value: 3,
enumerable: true // baz is an own enumerable property.
}
});
var copy = Object.assign({}, obj);
console.log(copy); // { baz: 3 }
console.log(obj);

obj结构如下

1
2
3
4
5
Object baz: 3
bar: 2
__proto__: Object
foo: 1
__proto__: Object

3.2 Object.setPrototypeOf(obj,prototype) prototype参数必须是一个对象或者null,否则会抛出异常,用来设置对象的 proto 的属性的指向,返回obj对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function Test(){
this.age = 18;
}
var test = new Test();
// console.dir(Test);
// console.log(Test.prototype);
var obj1 = Object.create(Test.prototype,{
'name':{
writable:true,
value:'JiM'
}
});
console.log(obj1);
console.log(obj1.__proto__ === Test.prototype );
var obj1 = Object.create(Object.prototype,{
'name1':{
writable:true,
value:'JiM1'
}
});
console.log(obj1);
console.log(obj1.__proto__ === Object.prototype );
var obj1 = Object.create(Array.prototype,{
'name2':{
writable:true,
value:'JiM2'
}
});
console.log(obj1);
console.log(obj1.__proto__ === Array.prototype );
var obj1 = Object.create(null,{
'name3':{
writable:true,
value:'JiM3'
}
});
console.log(obj1);
console.log(obj1.__proto__ === null );//false
console.log(obj1.__proto__ == null );//true
var o = {foo:"bar"}
var obj1 = Object.create(o,{
'name4':{
writable:true,
value:'JiM4'
}
});
console.log(obj1);
console.log(obj1.__proto__ === o );//true
var obj1 = Object.create(Test,{
'name5':{
writable:true,
value:'JiM5'
}
});
console.log(obj1);
console.log(obj1.__proto__ === Test);
//proto 设置obj1的__proto__ 指向,必须是一个对象或者null否则会抛出异常; prop 设置 obj1 的属性值
1
2
3
4
5
var obj = {name:"JHOn"};
console.log(obj);
// Object.setPrototypeOf(obj,null);
Object.setPrototypeOf(obj,Array.prototype);

4 Object.getOwnPropertyNames(obj )该函数可以将对象中可枚举以及不可枚举的key值都列举出来,返回一个key组成的数组

The object whose enumerable and non-enumerable own properties are to be returned.

An array of strings that correspond to the properties found directly upon the given object.

5 Object.keys(obj) 该方法返回obj对象的所有可枚举的属性的键值组成的数组,不可枚举的属性键不会返回

1
2
3
4
5
6
7
8
9
10
11
12
13
var obj = {name:"Jhon",age:12,gender:"man"};
Object.defineProperty(obj,"email",{
value:"Gemail",
enumerable:false //设置email为false不可枚举,便于检测
})
for(var key in obj ){ //遍历对象自身的和继承(__proto__上的属性)的可枚举的属性
console.log(key); //name age email
}
//以下两种方法只遍历对象的原生属性,不会遍历对象的原型__proto__上的属性
var arr = Object.getOwnPropertyNames(obj);
console.log(arr); //["name", "age", "gender", "email"]
var arr1 = Object.keys(obj);
console.log(arr1);//["name", "age", "gender"]

需要注意的一点,如果数组中有一项为undefined的话,我们需要理解Object.keys()底层的实现,其实是封装了for-in循环,以及判断是否有某个属性Object.prototype.hasOwnProperty() 方法.for-in循环用来获取对象或者数组的键名.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
if (!Object.keys) {
Object.keys = (function() {
'use strict';
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString'),
dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
],
dontEnumsLength = dontEnums.length;
return function(obj) {
if (typeof obj !== 'function' && (typeof obj !== 'object' || obj === null)) {
throw new TypeError('Object.keys called on non-object');
}
var result = [], prop, i;
for (prop in obj) {
if (hasOwnProperty.call(obj, prop)) {
result.push(prop);
}
}
if (hasDontEnumBug) {
for (i = 0; i < dontEnumsLength; i++) {
if (hasOwnProperty.call(obj, dontEnums[i])) {
result.push(dontEnums[i]);
}
}
}
return result;
};
}());
}

所以对于没有定义的某一项数组元素,Object.keys() 方法的输出如下,这点需要注意下

1
2
3
let Arr = ['a',,'c']
let sparseKeys = Object.keys(Arr)
console.log(sparseKeys) //["0","2"]